//
//  UIButton+HitEdgeInsets.m
//  YUNT
//
//  Created by 梁炜东 on 2017/11/14.
//  Copyright © 2017年 梁炜东. All rights reserved.
//

#import "UIButton+HitEdgeInsets.h"

#import <objc/runtime.h>

#define defaultInterval 1 //默认时间间隔为1s

@implementation UIButton (HitEdgeInsets)

static const NSString *KEY_HIT_TEST_EDGE_INSETS = @"HitTestEdgeInsets";
static const NSString *UIControl_eventTimeInterval = @"UIControl_eventTimeInterval";
//static const char *UIControl_enventIsIgnoreEvent = "UIControl_enventIsIgnoreEvent";

-(void)setHitTestEdgeInsets:(UIEdgeInsets)hitTestEdgeInsets
{
    NSValue *value = [NSValue value:&hitTestEdgeInsets withObjCType:@encode(UIEdgeInsets)];
    objc_setAssociatedObject(self, &KEY_HIT_TEST_EDGE_INSETS, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(UIEdgeInsets)hitTestEdgeInsets
{
    NSValue *value = objc_getAssociatedObject(self, &KEY_HIT_TEST_EDGE_INSETS);
    if(value)
    {
        UIEdgeInsets edgeInsets;
        [value getValue:&edgeInsets];
        return edgeInsets;
    }
    else
    {
        return UIEdgeInsetsZero;
    }
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    if(UIEdgeInsetsEqualToEdgeInsets([self hitTestEdgeInsets], UIEdgeInsetsZero) || ! self.enabled || self.hidden)
    {
        return [super pointInside:point withEvent:event];
    }
    CGRect hitFrame = UIEdgeInsetsInsetRect(self.bounds, [self hitTestEdgeInsets]);
    return CGRectContainsPoint(hitFrame, point);
}

/////////////////////////////////////////////////////
+(void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^
                  {
                      SEL selA = @selector(sendAction:to:forEvent:);
                      SEL selB = @selector(lwd_sendAction:to:forEvent:);
                      Method methodA = class_getInstanceMethod(self, selA);
                      Method methodB = class_getInstanceMethod(self, selB);
                      
                      BOOL isAdd = class_addMethod(self, selA, method_getImplementation(methodB), method_getTypeEncoding(methodB));
                      if (isAdd)
                      {
                          class_replaceMethod(self, selB, method_getImplementation(methodA), method_getTypeEncoding(methodA));
                      }else
                      {
                          //添加失败了 说明本类中有methodB的实现，此时只需要将methodA和methodB的IMP互换一下即可。
                          method_exchangeImplementations(methodA, methodB);
                      }
                  });
}
-(void)lwd_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
{
    self.eventTimeInterval = self.eventTimeInterval == 0 ? defaultInterval : self.eventTimeInterval;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.eventTimeInterval *NSEC_PER_SEC)), dispatch_get_main_queue(), ^
                   {
                       NSLog(@"");
                   });
    [self lwd_sendAction:action to:target forEvent:event];
}

-(NSTimeInterval)eventTimeInterval
{
    return [objc_getAssociatedObject(self, &UIControl_eventTimeInterval) doubleValue];
}
-(void)setEventTimeInterval:(NSTimeInterval)eventTimeInterval
{
    objc_setAssociatedObject(self, &UIControl_eventTimeInterval, @(eventTimeInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
